
package artistic;

import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.net.URL;
import javax.swing.Icon;
import javax.swing.ImageIcon;

/**
 *
 * @author Mikael Andersson
 * @author Rurik Högfeldt
 */

public class LineTool implements PaintTool
{
    private ToolBox toolBox;

    public LineTool(ToolBox toolBox)
    {
        this.toolBox = toolBox;
    }

    public Icon getIcon() {
        URL url = getClass().getResource("images/lineicon.png");
        return new ImageIcon(url);
    }

    private boolean isHeld = false;
    
    private int startX, startY;

    public void mouseCallback(PaintWindow target, MouseEvent e) {
        Brush brush = toolBox.getActiveBrush();
        
        int x = e.getX();
        int y = e.getY();

        PaintImage image = target.getImage();
        WorkCanvas canvas = image.createWorkLayer();
        
        if(e.getID() == MouseEvent.MOUSE_RELEASED)
        {
            if(isHeld)
            {
                brush.drawLine(canvas, startX, startY, x, y);
                canvas.done();
            }
            isHeld = false;
        }
            
        if(e.getID() == MouseEvent.MOUSE_PRESSED)
        {
            isHeld = true;
            startX = x;
            startY = y;
        }

        target.repaint();
    }

    public void mouseWheelCallback(PaintWindow target, MouseWheelEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

}
